import { Entity, Column, ManyToOne, JoinColumn, OneToMany } from "typeorm";

import { INTEGER_PG, VARCHAR255_PG, TonTplGroup, TonTpl, TonTplGateway, TonTplGroupGateway } from "qqlx-core";

import { TransformerInteger } from "@lib/orm.transfor.number";
import { TransformerVarchar255 } from "@lib/orm.transfor.string";

import { IdIntroSchema } from "@/alpha/ext.id.intro";
import { SkuTplSchema } from "@/alpha/ext.com.tpl";

@Entity({ name: TonTplGroupGateway.relations })
export class TonTplGroupSchema extends IdIntroSchema implements TonTplGroup {
  @Column({ transformer: new TransformerVarchar255() })
  formulaName: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerVarchar255() })
  formulaContent: VARCHAR255_PG = "";

  @OneToMany("TonTplSchema", (s: TonTpl) => s.justTonTplGroup)
  joinTonTplList!: TonTpl[];
}

@Entity({ name: TonTplGateway.relations })
export class TonTplSchema extends SkuTplSchema implements TonTpl {
  @Column({ transformer: new TransformerVarchar255() })
  ivid: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerVarchar255() })
  labelStr: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerInteger() })
  refTonTplGroupId!: INTEGER_PG;

  @ManyToOne("TonTplGroupSchema", (s: TonTplGroup) => s.joinTonTplList)
  @JoinColumn({ name: "refTonTplGroupId", referencedColumnName: "id" })
  justTonTplGroup!: TonTplGroup;
}
